<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 使用一个闭包函数,可以设置和获取内存,但是没办法直接获取内存 */
        function createCache() {
            const data = {}         // 闭包中的数据 只要未被return 外部就没办法访问
            return {
                set: function (key, val) {
                    data[key] = val
                },
                get: function (key) {
                    return data[key]
                }
            }
        }

        var obj = createCache();
        obj.set("name", "cheng");
        obj.set("age", 12);
        console.log(obj.get("name"), obj.get("age"));
    </script>
</body>

</html>